package com.ks.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ks.common.Const;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.*;

/**
 * @author
 */
public class ConfigService {

    private static final Logger logger =  Logger.getLogger(ConfigService.class);

    private final static Properties props = new Properties();

    private static long configLastModifyTime = 0;

    private static long serverLastModifyTime = 0;

    public static void init() {

    }
    static  {
        loadProps();
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                loadProps();
            }
        };
        timer.schedule(task, 0, 2000);
    }

    private static void loadProps() {

        File confFile = new File(System.getProperty("user.dir"), "config.properties");
        File serverConfFile = new File(System.getProperty("user.dir"), "server_conf.json");

        if (configLastModifyTime == confFile.lastModified()
                && serverLastModifyTime == serverConfFile.lastModified()) return;

        if (configLastModifyTime != confFile.lastModified()) {
            configLastModifyTime = confFile.lastModified();
            InputStreamReader reader = null;
            try {
                reader = new InputStreamReader(new FileInputStream(confFile.toString()), "utf-8");
                props.load(reader);
                Const.teamQQEmail = ConfigService.getProperty("teamQQEmail", "");
                Const.scheduleTask= ConfigService.getIntProperty("schedule_task", 0) > 0;
                Const.emailLimit= ConfigService.getIntProperty("emailLimit", 0);
                Const.failMaxCount= ConfigService.getIntProperty("failMaxCount", 3);

                logger.info("config_file success");
            } catch (FileNotFoundException ignored) {
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }catch (IOException e) {
                logger.error("config_file error");
            }
        }

        if (serverLastModifyTime != serverConfFile.lastModified()) {
            serverLastModifyTime = serverConfFile.lastModified();
            InputStreamReader reader6;
            try {
                reader6 = new InputStreamReader(new FileInputStream(serverConfFile.toString()), "utf-8");
                char buf[] = new char[655360];
                int len;
                if ((len = reader6.read(buf, 0, buf.length)) > 0) {
                    JSONObject root = JSON.parseObject(new String(buf, 0, len));

                    JSONArray serverArray = root.getJSONArray("server");
                    MonitorManager.getInstance().init(serverArray);
                }

                reader6.close();

                logger.info("server_conf success");

            } catch (Exception e) {
                logger.info("server_conf fail");
            }
        }
    }


    public static String getProperty(String key) {
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultVal) {
        return props.getProperty(key, defaultVal);
    }

    public static int getIntProperty(String key) {
        String val = props.getProperty(key);
        try {
            return Integer.parseInt(val);
        } catch (NumberFormatException e) {
            logger.info("Get config error! key : " + key, e);
            return 0;
        }
    }

    public static int getIntProperty(String key, int defaultVal) {
        String val = props.getProperty(key);
        if (val == null) {
            return defaultVal;
        }
        try {
            return Integer.parseInt(val);
        } catch (NumberFormatException e) {
            logger.info("Get config error! key : " + key, e);
            return defaultVal;
        }
    }

    public static boolean getBoolProperty(String key) {
        String val = props.getProperty(key);
        try {
            return Boolean.parseBoolean(val);
        } catch (NumberFormatException e) {
            logger.info("Get config error! key : " + key, e);
            return false;
        }
    }

    public static boolean getBoolProperty(String key, boolean defaultVal) {
        String val = props.getProperty(key);
        try {
            return Boolean.parseBoolean(val);
        } catch (NumberFormatException e) {
            logger.info("Get config error! key : " + key, e);
            return defaultVal;
        }
    }

}